//
//  NSString+PPString.m
//  pickpic
//
//  Created by 胡文广 on 2017/3/25.
//  Copyright © 2017年 MicroDream. All rights reserved.
//

#import "NSString+PPString.h"

@implementation NSString (PPString)
-(BOOL) isValid
{
    return ([[self class]isSubclassOfClass:[NSNull class]] || [[self toTrim] isEqualToString:@""] || self == nil || [self isEqualToString:@"(null)"] || [self isEqualToString:@"null"] || [self isEqualToString:@"<null>"]) ? NO :YES;
}
-(NSString*) toTrim
{
    NSString *trimmedString = [self stringByTrimmingCharactersInSet:
                               [NSCharacterSet whitespaceAndNewlineCharacterSet]];
    return trimmedString;
}

//得到字节数函数
+ (NSUInteger)stringConvertToInt:(NSString*)strtemp
{
    //    int strlength = 0;
    //    char* p = (char*)[strtemp cStringUsingEncoding:NSUnicodeStringEncoding];
    //    for (int i=0 ; i<[strtemp lengthOfBytesUsingEncoding:NSUnicodeStringEncoding] ;i++)
    //    {
    //        if (*p) {
    //            p++;
    //            strlength++;
    //        }
    //        else {
    //            p++;
    //        }
    //    }
    //    return (strlength+1)/2;
    
    
    NSUInteger asciiLength = 0;
    for (NSUInteger i = 0; i < strtemp.length; i++) {
        unichar uc = [strtemp characterAtIndex: i];
        asciiLength += isascii(uc) ? 1 : 2;
    }
    NSUInteger unicodeLength = asciiLength;
    return unicodeLength;
    
}
//判断字体是否加载
//在使用字体文件前最好是先判断字体是否已经被加载过了
+ (BOOL)isFontDownloaded:(NSString *)fontName
{
    UIFont* aFont = [UIFont fontWithName:fontName size:12.0];
    BOOL isDownloaded = (aFont && ([aFont.fontName compare:fontName] == NSOrderedSame || [aFont.familyName compare:fontName] == NSOrderedSame));
    return isDownloaded;
}


/**
 * 字体注册
 */
- (void)registerFontPath
{
        if ([self isEqualToString:@"36ufbxg25n812fc2bws6o2en_URL8.TTF"]) {
            return;
        }
    
    NSString *library = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,YES) objectAtIndex:0];
    
    NSString *fileDirectory = [library stringByAppendingPathComponent:self];
    
    CFErrorRef error;
    CGDataProviderRef providerRef = CGDataProviderCreateWithFilename([fileDirectory UTF8String]);
    if (!providerRef) {
        return;
    }
    CGFontRef font = CGFontCreateWithDataProvider(providerRef);
    if (! CTFontManagerRegisterGraphicsFont(font, &error))
    {
        //注册失败
        CFStringRef errorDescription = CFErrorCopyDescription(error);
        NSLog(@"Failed to load font: %@", errorDescription);
        CFRelease(errorDescription);
    }
    if (font) {
        
        CFRelease(font);
    }
    if (providerRef) {
        
        CFRelease(providerRef);
    }
    
}
@end
